package com.code.app.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import com.code.app.R;

public class TextView extends View {

    private String text;

    private int textColor;

    private int textSize;

    private Paint paint;

    public TextView(Context context) {
        this(context, null);
    }


    public TextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextView);
        text = typedArray.getText(R.styleable.TextView_text).toString();
        @SuppressLint("ResourceAsColor")
        int textColor = typedArray.getColor(R.styleable.TextView_textColor, android.R.color.black);
        int textSize = typedArray.getDimensionPixelSize(R.styleable.TextView_textSize, 0);
        //一定要回收
        typedArray.recycle();

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(textSize);
        paint.setColor(textColor);
    }

    private int sp2px(int size) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, size, getResources().getDisplayMetrics());
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取宽高模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);



//        判断测量模式
//        如果mode是EXATLY（wrap_content 100dp）不用管 直接用户是设置多少就是多少
//        如果mode是AT_MOST 要判断用户设置的宽高
        if (widthMode == MeasureSpec.AT_MOST) {
//        用画笔测量文字的宽高 有字体大小和宽高决定
            Rect bunds = new Rect();
//            得到文字的矩形区域
            paint.getTextBounds(text, 0, text.length(), bunds);
            width = getPaddingRight() + getPaddingLeft() + bunds.width();
        }
        if (heightMode == MeasureSpec.AT_MOST) {
//          用画笔测量文字的宽高 有字体大小和宽高决定
            Rect bunds = new Rect();
//          得到文字的矩形区域
            paint.getTextBounds(text, 0, text.length(), bunds);
            height = getPaddingTop() + getPaddingBottom() + bunds.height();
        }
//        设置宽高
        setMeasuredDimension(width, height);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//      Paint.FontMetrics和Paint.FontMetricsInt这两个方法是一样的，只是后者取到的值是一个整形。
        Paint.FontMetricsInt metricsInt = paint.getFontMetricsInt();
        int dy = (metricsInt.bottom - metricsInt.top) / 2 - metricsInt.bottom;
        int baseline = getHeight() / 2 + dy;
        canvas.drawText(text, getPaddingLeft(), baseline, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }
}
